﻿using UnityEngine;

namespace winS.UnityEditor.UIElement
{
    public static class ColorLevelUtility
    {
        public const byte interval = 11;

        public static readonly int maxLevel;
        public static readonly float intervalRatio;

        static ColorLevelUtility()
        {
            maxLevel = (int)(255f / interval);
            intervalRatio = interval / 255f;
        }

        public static Color GetColor(int level)
        {
            return GetColor(level, 255);
        }
        public static Color GetColor(int level, byte alpha)
        {
            if (level < 0 || level > maxLevel) throw new System.Exception($"{nameof(ColorLevelUtility)}.{nameof(GetColor)}: 层级范围应该在 [0,{maxLevel}] 之间");
            byte colorValue = (byte)(level * interval);
            return new Color32(colorValue, colorValue, colorValue, alpha);
        }
        public static Color GetNext(Color color)
        {
            return new Color(Mathf.Min(1f, color.r + intervalRatio), Mathf.Min(1f, color.g + intervalRatio), Mathf.Min(1f, color.b + intervalRatio), color.a);
        }
        public static Color GetPrevious(Color color)
        {
            return new Color(Mathf.Max(0f, color.r - intervalRatio), Mathf.Max(0f, color.g - intervalRatio), Mathf.Max(0f, color.b - intervalRatio), color.a);
        }
    }
}